package model

import (
	"Family/app/dot"
	"Family/app/entity"
	"Family/library"
)
type MemberModel struct {
}
func (member *MemberModel) AddMember(model *entity.Member) bool {
	result := library.Db.Exec("CALL AddMember(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.Nickname, model.Gender, model.Birthdate,
		model.Deathdate, model.Marriagedate, model.Intro, model.MateId, model.FatherId, model.MotherId, model.CreateTime)
	if result.Error == nil {
		return true
	}
	return false
}

func (member *MemberModel) FindMemberById(memberId string) *entity.Member {
	var model entity.Member
	library.Db.Model(&model).Where("id = ?", memberId).First(&model)
	return &model
}

func (member *MemberModel) UpdateMemberById(memberId string, data map[string]interface{}) bool {
	var model entity.Member
	result := library.Db.Model(model).Where("id = ?", memberId).Updates(data)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

func (member *MemberModel) GetList(param dot.GetListReq) ([]entity.Member, int64) {
	if param.Page == 0 {
		param.Page = 1
	}

	if param.Page == 0 {
		param.Page = 10
	}

	offset := (param.Page - 1) * param.Limit
	var count int64
	var model []entity.Member
	library.Db.Model(model).Count(&count).Order("id DESC").Offset(offset).Limit(param.Limit).Find(&model)
	return model, count
}

func (member *MemberModel) Delete(memberId string) bool {
	var model entity.Member
	result := library.Db.Delete(&model, memberId)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}
